package com.bw.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;
import com.bw.user.entity.HouseUserEntity;
import com.bw.user.entity.UserCommentEntity;
import com.bw.user.service.HouseUserService;
import com.bw.user.service.UserCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 
 *
 * @author Hao_YunKai
 * @email 2745578447@qq.com
 * @date 2022-04-01 19:00:40
 */
@RestController
@RequestMapping("user/user-comment")
public class UserCommentController {
    @Autowired
    private UserCommentService userCommentService;

    @Autowired
    private HouseUserService houseUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userCommentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{commentId}")
    public R info(@PathVariable("commentId") Long commentId){
		UserCommentEntity userComment = userCommentService.getById(commentId);

        return R.ok().put("userComment", userComment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserCommentEntity userComment){
        userComment.setCommentTime(new Date());
        userComment.setUserName(houseUserService.getOne(new QueryWrapper<HouseUserEntity>().eq("user_phone", userComment.getUserPhone())).getUserName());
        userComment.setCommentId(IdWorker.getId());

		userCommentService.save(userComment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserCommentEntity userComment){
		userCommentService.updateById(userComment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] commentIds){
		userCommentService.removeByIds(Arrays.asList(commentIds));

        return R.ok();
    }
}
